<?php
include_once '../../lib/config.php';
include_once '../../lib/db.php';
include '../../lib/utils/date.php';
include '../../lib/utils/tag.php';
include '../../model/departemen.php';
include '../../model/karyawan.php';
include '../../model/rangking.php';
include '../../model/perbuatan.php';
include '../../model/report.php';
include '../../model/report2.php';

$proc = $_REQUEST['proc'];

switch ($proc):
case 'search-kary':
	include '../../view/admin/totalPointKaryawan/searchKary.php';
	break;

case 'search-kary-table':
	$searchKey = $_REQUEST['searchKey'];
	$departemenID = $_REQUEST['departemenID'];
	$statusID = $_REQUEST['statusID'];
	include '../../view/admin/totalPointKaryawan/searchKaryTableList.php';
	break;

case 'search-kary-pick':
	$karyID = $_REQUEST['kary_id'];
	include '../../view/admin/totalPointKaryawan/jabatanCombo.php';
	break;
case 'graph':
	include ("../../lib/jpgraph/jpgraph.php");
	include ("../../lib/jpgraph/jpgraph_line.php");
	
	//generate data
	$startPeriode = $_REQUEST['periode1'];
	$endPeriode = $_REQUEST['periode2'];
	
	$kary_id = $_REQUEST['kary_id'];
	$kary_dept = $_REQUEST['kary_dept'];
	
	$startYear  = date("Y", $startPeriode) + 0;
	$startMonth = date("m", $startPeriode) + 0;
	$endYear    = date("Y", $endPeriode) + 0;
	$endMonth   = date("m", $endPeriode) + 0;
	$graphData = array();
	
	if (!$kary_id || !$kary_dept || !$startPeriode || !$endPeriode) {
		return;
	}
	
	$CSIM = array();
	$graphData['kary'][] = $graphData['label'][] = $CSIM['kary']['target'][] = $CSIM['kary']['alt'][] = "";
	foreach (rep_total_poin_karyawan($kary_id, $startPeriode, $endPeriode) as $tmp=>$value){
		$graphData['kary'][] = $value;
		$graphData['label'][] = $tmp;
		$sp = split('-',$tmp);
		$CSIM['kary']['target'][] = "load_drillGraph(".json_encode(array(
				"type"=> '1',
				'kary_id'=> $kary_id,
				'kary_dept'=> $kary_dept,
				'month'=> $sp[0],
				'year'=> $sp[1],
			)).")";
		$CSIM['kary']['alt'][] = "%0.1f";
	}
	$graphData['kary'][] = $graphData['label'][] = $CSIM['kary']['target'][] = $CSIM['kary']['alt'][] = "";
	
	$graphData['dept'][] = $CSIM['dept']['target'][] = $CSIM['dept']['alt'][] = "";
	foreach (rep_rata_departemen($kary_dept, $startPeriode, $endPeriode) as $tmp=>$value){
		$graphData['dept'][] = $value;
		$sp = split('-',$tmp);
		$CSIM['dept']['target'][] = "load_drillGraph(".json_encode(array(
				"type"=> '-1',
				'kary_dept'=> $kary_dept,
				'month'=> $sp[0],
				'year'=> $sp[1],
			)).")";
		$CSIM['dept']['alt'][] = "%0.1f";
	}
	$graphData['dept'][] = $CSIM['dept']['target'][] = $CSIM['dept']['alt'][] = "";
	
	// Create the linear plot
	$line1 = __createLinePlot($graphData['kary'], 
		array('color'=>'blue','weight'=>2), 
		array('type'=>MARK_FILLEDCIRCLE, 'fillColor'=>"blue",'width'=>4), 
		array('color'=>"darkblue",'font'=>array(FF_FONT1, FS_BOLD), 'show'=>true), 
		array('title'=>"Karyawan"), 
		$CSIM['kary']);
	$line2 = __createLinePlot($graphData['dept'], 
		array('color'=>'red','weight'=>2), 
		array('type'=>MARK_SQUARE, 'fillColor'=>"red",'width'=>6), 
		array('color'=>"darkred",'font'=>array(FF_FONT1, FS_BOLD), 'show'=>true), 
		array('title'=>"Rata-rata Departemen"),
		$CSIM['dept']);
	
	// Create the graph.
	$graph = new Graph(700,500,"auto");
	$graph->SetFrame(false,'white@.5',1); 
	$graph->title->Set("Total Point per Karyawan");
	$graph->SetColor("white"); 
	$graph->SetScale("textlin");
	$graph->img->SetMargin(80,30,40,80);
	$graph->img->SetAntiAliasing(); 
	
	$graph->ygrid->SetFill(true,'#FFFFFF@0.5','#d7e5f5@0.5');
	$graph->yaxis->SetFont(FF_FONT1,FS_BOLD);
	$graph->yaxis->SetPos("min");
	$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
	$graph->yaxis->title->set("Point");
	$graph->yaxis->SetTitleMargin(50);
	
	$graph->xaxis->SetFont(FF_FONT1,FS_BOLD);
	$graph->xaxis->SetPos("min");
	$graph->xaxis->SetTickLabels($graphData['label']);
	$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
	$graph->xaxis->title->set("Periode");
	if ( count($graphData['label'])>10 ) {
		$graph ->xaxis->SetTextLabelInterval(count($graphData['label'])/4);
	}
	
	$graph->legend->SetLayout(LEGEND_HOR);
	$graph->legend->Pos(0.4,0.95,"center","bottom");
	
	// Add the plot to the graph
	$graph->Add($line1);	
	$graph->Add($line2);
	
	// Display the graph
	$graph->StrokeCSIM('proc/admin/totalPointKaryawan.php');
	break;
	
case 'graph-drill-karyawan':
	$kary_id = $_REQUEST['kary_id'];
	$month = $_REQUEST['month'];
	$year = $_REQUEST['year'];
	include '../../view/admin/totalPointKaryawan/drill-karyawan.php';
	break;
	
case 'graph-drill-departemen':
	$kary_dept = $_REQUEST['kary_dept'];
	$month = $_REQUEST['month'];
	$year = $_REQUEST['year'];
	$karyList = kary_searchByDepartemen("", $kary_dept);
	
	include '../../view/admin/totalPointKaryawan/drill-departemen.php';
	break;
endswitch;